/* Licence:
*   Use this however/wherever you like, just don't blame me if it breaks anything.
*
* Credit:
*   If you're nice, you'll leave this bit:
*
*   Class by Pierre-Alexandre Losson -- http://www.telio.be/blog
*   email : plosson@users.sourceforge.net
*/
function updateProgress(uploadInfo)
{
    if (uploadInfo.inProgress)
    {
        document.getElementById('uploadbutton').disabled = true;
        document.getElementById('file1').disabled = true;

        var fileIndex = uploadInfo.fileIndex;

        var progressPercent = Math.ceil((uploadInfo.bytesRead / uploadInfo.totalSize) * 100);

        document.getElementById('progressBarText').innerHTML = 'upload in progress: ' + progressPercent + '%';

        document.getElementById('progressBarBoxContent').style.width = parseInt(progressPercent * 3.5) + 'px';

        window.setTimeout('refreshProgress()', 1000);
    }
    else
    {
        document.getElementById('uploadbutton').disabled = false;
        document.getElementById('file1').disabled = false;
    }

    return true;
}

function push(event) {
	// For this example, we simply show the excuse on the page.
	//document.getElementById('excuse').innerHTML = event;
	alert(event);
}
// This function will be called when/if the stream closes.
function disconnected() {
    // For this example we'll just show a nice message.
	alert('disconnected.');
	//document.getElementById('excuse').innerHTML = '<img src="img/arrow.png" alt="" title="" />'+' Click to see MORE reasons why this souldn\'t work!';
	//document.getElementById('control').value = 'start';
}
//var comet = new comet();
// This function is executed when the button is clicked.
function toggle() {
	// First we check if the stream is open.
	if (!comet.active) {
		// Lets start streaming!
		new Function();
		comet.open('/cmt/pgr/upload?rnd='+ new Date(), push, disconnected);
		//document.getElementById('control').value = 'stop';
	} else {
		// Streaming is active, means the user wants to stop it.
		//comet.close();
	} 
}
function startProgress()
{	
    /*document.getElementById('progressBar').style.display = 'block';
    document.getElementById('progressBarText').innerHTML = 'upload in progress: 0%';
    //document.getElementById('uploadbutton').disabled = true;
    $.post('/cmt/pgr/upload',{},function(data){
		alert(data);
	});*/
    // wait a little while to make sure the upload has started ..
    //window.setTimeout("refreshProgress()", 1500);*/
	$.post('/cmt/pgr/upload?r='+ Math.random(), function(data) {
		   	alert(data);
	});
	//jp.open('/cmt/pgr/upload?rnd='+ new Date(), push);
    return true;
}
$(document).ready(function(){
	$('#upload_iframe').load(function(event){
		   var data = null;
		   function getIFrameDOM(id){
		        return document.getElementById(id).contentDocument || document.frames[id].document;
		   }
		   var doc = getIFrameDOM('upload_iframe');
		   var jsonParentNode = doc.getElementsByTagName('pre');
		   if(jsonParentNode.length > 0 ){
			   data = jsonParentNode[0].innerHTML;
		   }
		   /*if(document.all){
			   data = doc.getElementsByTagName('body')[0].innerHTML;
		   }else{
			   var jsonParentNode = doc.getElementsByTagName('pre');
			   if(jsonParentNode.length > 0 ){
				   data = jsonParentNode[0].innerHTML;
			   }
		   }*/
		   if(!data){
			   return;
		   }
		   alert(data);
		   var urlJson = jQuery.parseJSON(data);
		   var idVal= $('#imageCount').html();
		   var idx = parseInt(idVal);
		   idx++;
		   var imageId = '#selImg'+ idVal;
		   $(imageId).attr('src',urlJson.url);
		   $('#imageCount').html(idx);
	});
});
